package top.shen33.yang.services.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.shen33.yang.mapper.EdoPostReplyMapper;
import top.shen33.yang.model.entity.EdoPost;
import top.shen33.yang.model.entity.EdoPostReply;
import top.shen33.yang.services.EdoPostReplyService;

import java.util.List;

/**
 * <p>
 * 回复 服务实现类
 * </p>
 *
 * @author oreilly
 * @since 2018-08-30
 */
@Service
public class EdoPostReplyServiceImpl extends ServiceImpl<EdoPostReplyMapper, EdoPostReply>implements EdoPostReplyService {


    @Override
    public Class clazz() {
        return EdoPostReply.class;
    }

    @Override
    @Transactional
    public void doAdd(EdoPostReply itemEdoPostReply) {

        itemEdoPostReply.insertOrUpdate();

        //数量
        Integer postId = itemEdoPostReply.getPostId();
        EdoPost edoPost = new EdoPost().selectById(postId);
        Integer replyNum = edoPost.getReplyNum();
        if(replyNum == null){
            replyNum = 0;
        }
        replyNum++;
        edoPost.setReplyNum(replyNum);

        edoPost.insertOrUpdate();




    }


    @Override
    @Transactional
    public void doUpdate(EdoPostReply itemEdoPostReply) {

        itemEdoPostReply.insertOrUpdate();
    }

    @Override
    @Transactional
    public void doDelete(String[] ids) {

        QueryWrapper<EdoPostReply> ew = new QueryWrapper<>();
        ew.in("sid", ids);
        new EdoPostReply().delete(ew);
    }

    @Override
    public EdoPostReply getOne(String sid) {

        //关联查询

        EdoPostReply itemEdoPostReply = getById(sid);

        return itemEdoPostReply;
    }

    @Override
    public List<EdoPostReply> all(EdoPostReply itemEdoPostReply) {

        return this.baseMapper.all(itemEdoPostReply);
    }


        @Override
    public Page<EdoPostReply> pageInfo(Page page, EdoPostReply itemEdoPostReply) {

        List<EdoPostReply> dataList = this.baseMapper.pageInfo(page, itemEdoPostReply);
        if(page ==null ){
            page = new Page(0,10000);
        }
        page.setRecords(dataList);
        return page;
    }


    @Override
    @Transactional
    public void doImport(List<EdoPostReply> dataList) {

        //数据验证

        //保存
        this.saveOrUpdateBatch(dataList);
    }

}
